import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

class Solution {
/*    public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }*/
  public List<List<Integer>> levelOrder(TreeNode root) {
        if(root==null){
            return null;
        }
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        List<List<Integer>> lists=new ArrayList<>()
        while(!queue.isEmpty()){
          
            int size=queue.size();
            List<Integer> list=new ArrayList<>();
            while(size!=0){
                size--;
                TreeNode node =queue.poll();
                list.add(node.val);
                if(root.left!=null){
                    queue.offer(root.left);
                }
                if(root.right!=null){
                    queue.offer(root.right);
                }
            }
            lists.add(list);
            }
        return lists;



    }
}